<template>
  <div class="designer">
    <el-dialog :visible.sync="visible" title="变更设计师" :close-on-click-modal="false" @close="handleBack">
      <c-form-container :column="1" :bottom="false">
        <el-form ref="form" :model="formData" :rules="formRules" label-width="90px">
          <el-form-item label="当前设计师">
            <div>{{ oldDesigner }}</div>
          </el-form-item>
          <el-form-item prop="person" label="变更后设计师">
            <input-with-dialog
              v-model="formData.staff"
              :props="{label:'userName',value:'account'}"
              :multiple="false"
              :focus-query="true"
              :config="designerConfig"
              @change="designerChange"
            />
          </el-form-item>
        </el-form>
      </c-form-container>
      <div slot="footer">
        <el-button size="mini" @click="handleBack">取消</el-button>
        <el-button type="primary" size="mini" @click="confirmHandle">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getProConfigEditData, submitConfigTable } from '@/api/proConfig'
export default {
  name: 'AddSample',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      oldDesigner: '',
      formData: {
        staff: ''
      },
      formRules: {},
      designerConfig: {
        title: '设计师',
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/user/listUsers`,
        setting: [
          {
            prop: 'account',
            title: '编码'
          },
          {
            prop: 'userName',
            title: '名称'
          }
        ]
      }
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    this.formData.id = this.detailData.id
    getProConfigEditData(this.formData.id).then(res => {
      if (res.success && res.data) {
        this.formData = res.data
        this.oldDesigner = this.formData.proConfiguratorName
        // const otherFilter = [{ 'id': 'officeCode', 'property': 'officeCode', 'operator': '=', 'value': this.formData.officeCode },
        //   { 'id': 'postType', 'property': 'postType', 'operator': '=', 'value': 'CUSTOMIZE' },
        //   { 'id': 'isValid', 'property': 'isValid', 'operator': '=', 'value': '1' }]
        // this.$set(this.designerConfig, 'filtersRaw', otherFilter)
      }
    })
  },
  methods: {
    designerChange(row) {
      this.formData.proConfiguratorCode = row[0].account
      this.formData.proConfiguratorName = row[0].userName
    },
    // 返回
    handleBack() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        staff: []
      }
      this.oldDesigner = ''
      this.$refs.rulesForm && this.$refs.rulesForm.clearValidate()
      done && done()
    },
    confirmHandle() {
      this.$refs.form.validate(validate => {
        if (validate) {
          submitConfigTable(this.formData).then(res => {
            if (res.success) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.dialogShow = false
              this.clearForm()
              const isNew = !this.formData.id // 是否新增数据 true (新增则刷新列表到第一页)/ false (非新增则刷新当前页)
              this.$emit('success', isNew)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.designer{
  display: inline-block;
}
</style>
